// ***************     I2C module by Berty    ***************
#include "iic.h"

// ******************************************** //
// *** This routine will make a delay       *** //
// ******************************************** //
void haltb()
	{
	unsigned char a;

	for (a = 1; a<250; a++);
		for (a = 1; a<250; a++);

//	_delay_ms(20);
	}

// ******************************************** //
// *** This routine will make a delay       *** //
// ******************************************** //
void halta()
	{

	unsigned char a;

	for (a = 1; a<250; a++);

//	_delay_ms(5);
	}
	
// ******************************************** //
// *** This routine will setup the I2C Port *** //
// ******************************************** //


// ************************************************ //
// *** This routine will send the I2C Start Bit *** //
// ************************************************ //

void I2C_Start(void)
{
 SETBIT(I2C_DIR,SCL);   	   // Set SCL to output	
 NOP();
 NOP();
 NOP();
 NOP();
 SETBIT(I2C_DIR,SDA);		   // Set SDA to output
 NOP();
 NOP();
 NOP();
 NOP();
 SETBIT(I2C_PORT,SCL);				// Set SCL High
 NOP();
 NOP();
 NOP();
 NOP();
 SETBIT(I2C_PORT,SDA);				// Set SDA High
 halta();
 CLEARBIT(I2C_PORT,SDA);		   // Clear SDA
 NOP();
 NOP();
 NOP();
 NOP();
}


// *********************************************** //
// *** This routine will send the I2C Stop Bit *** //
// *********************************************** //

void I2C_Stop (void)
{
 
 SETBIT(I2C_DIR,SDA);   		   // Set SDA to output
	
 CLEARBIT(I2C_PORT,SDA);		   // Clear SDA
 NOP();
 NOP();
 NOP();
 NOP();
 CLEARBIT(I2C_PORT,SCL);		   // Clear SCL
 halta();		
 SETBIT(I2C_PORT,SCL);			   // Set SCL High
 halta();
 SETBIT(I2C_PORT,SDA);			   // Set SDA High

//set to inpits direction
 CLEARBIT(I2C_DIR,SDA);
 CLEARBIT(I2C_DIR,SCL);
}





// ************************************************** //
// *** This routine will send the I2C clock pulse *** //
// ************************************************** //

void I2C_Clock (void)
{
 SETBIT(I2C_PORT,SCL);				// Set SCL high
 NOP();								// Small Delay
 halta();
 CLEARBIT(I2C_PORT,SCL);			// Set SCL low
}

// ************************************************** //
// *** This routine will send the I2C clock pulse *** //
// ************************************************** //

void I2C_Rack (void)
{
 SETBIT(I2C_DIR,SDA);				// Set SDA to output
 CLEARBIT(I2C_PORT,SDA);		  	// Clear I2C SDA PIN
 NOP();								// Small Delay
 SETBIT(I2C_PORT,SCL);				// Set SCL high
 halta();
 CLEARBIT(I2C_PORT,SCL);			// Set SCL low
}

// ******************************************************** //
// *** This routine will write a byte to the I2C device *** //
// ******************************************************** //

void Write_I2C_Byte(unsigned char byte)
{
 unsigned char i;

 SETBIT(I2C_DIR,SDA);   		   // Set SDA to output	
 CLEARBIT(I2C_PORT,SCL);

 for (i = 0; i < 8; i++){  		 // Loop for our 8 bits
 
	// Set or Clear SDA pin
	 if((byte & 0x80) == 0x80) SETBIT(I2C_PORT, SDA);	// Set I2C SDA PIN
	 else CLEARBIT(I2C_PORT,SDA);	
		  				// Clear I2C SDA PIN
	 halta();
	 SETBIT(I2C_PORT,SCL);			   // Set SCL High, Clock data
	  
	 halta();
	 byte = byte << 1;	// Shift data in buffer right one
				  
	
     CLEARBIT(I2C_PORT,SCL);		   // Clear SCL

	}

}

// ********************************************************* //
// *** This routine will read a byte from the I2C device *** //
// ********************************************************* //

unsigned char Read_I2C_Byte(void)
{	
 unsigned char i,buff = 0;
	CLEARBIT(I2C_DIR,SDA);   		   // Set SDA to input
	for (i = 0; i < 8; i++){
	
		buff = buff << 1;
		 				
		SETBIT(I2C_PORT,SCL);	  	  	   // Set SCL High,Clock bit out
		halta();
		// Read data on SDA pin
		if ( CHECKBIT( I2C_IN,SDA ) ) buff = buff | 0x01;	
		
		CLEARBIT(I2C_PORT,SCL);	  	  	   // Clear SCL 	
        }
	
 return buff;							   // Return our I2C byte
}




// *********************************************************************** //
// *** This routine returns a 0 if the I2C device sends an acknowledge *** //
// *********************************************************************** //

unsigned char I2C_Ack(void)
{
 unsigned char Ack = 0;		   // Temp RAM for Ackn flag
	
 CLEARBIT(I2C_PORT,SCL);
 CLEARBIT(I2C_DIR,SDA);   		   // Set SDA to input	
 
 SETBIT(I2C_PORT,SCL);			   // Clock the ACK bit
  halta();
 if ( CHECKBIT( I2C_IN,SDA ) ) Ack = 1;	  // Check the ACK bit on SDA	
 
 CLEARBIT(I2C_PORT,SCL);	  	   // Clear the clock

 return Ack;					   // Return our ACK bit
}	

